class EditionCreditsController < ApplicationController
  #filter_access_to :all
  #If AJAX request, use the slim layout (no header or footer)
  #Otherwise use the main layout
  layout Proc.new { |controller| controller.request.xhr? ? 'slim' : 'main' }

  # GET /editions/:edition_id/edition_credits/new
  def new
    @edition = Edition.find(params[:edition_id])
  end
  
  # GET /editions/:edition_id/edition_credits/search
  def search
    #@edition = Edition.find(params[:edition_id])
	@artists = Artist.search(params[:search])
	
	respond_to do |format|
	  format.html { }
	  format.js {
	    render :update do |page|
		  page.replace_html("edition-credits-search-results", :partial => 'results',
		                                                       :locals => { :artists => @artists })
		end
	  }
	end
  end

  # POST /editions/:edition_id/edition_credits
  def create
    #Create a new credit with the provided details
    @edition = Edition.find(params[:edition_id])
	credit = EditionCredit.new(params[:edition_credit])
	credit.edition = @edition
	
	#If an artist id was passed along, set it as the credits artist
	if params[:artist_id]
	  credit.artist_id = params[:artist_id]
	  
	#Otherwise a name was passed, which means create a new artist
	else
	  new_artist = Artist.new(:name => params[:artist_name])
	  credit.artist = new_artist
	end
	
	respond_to do |format|
	  if credit.save
	    flash[:notice] = 'Credit successfully added.'
		format.html { redirect_to edit_release_edition_path(@edition.release, @edition) }
	  else
	    flash[:error] = credit.errors.full_messages
		format.html { redirect_to edit_release_edition_path(@edition.release, @edition) }
	  end
	end
	
  end

  # GET /edition_credits/:edition_id/edit
  def edit
    @edition_credit = EditionCredit.find(params[:id])
  end

  # PUT /edition_credits/:edition_id
  def update
    @edition_credit = EditionCredit.find(params[:id])

    #Update the credit details
	respond_to do |format|
      if @edition_credit.update_attributes(params[:edition_credit])
        flash[:notice] = 'Credit successfully updated.'
        format.html { redirect_to_request_uri }
		format.js {
		  render :update do |page|
		    page.replace_html("edition-credit-#{@edition_credit.id}", :partial => 'editions/edit_edition_credit',
			                                                          :locals => { :credit => @edition_credit })
		  end
		}
      else
        flash[:errors] = 'Failed to update credit, please try again.'
        format.html { render :edit }
		format.js { }
      end
	end
  end

  # DELETE /edition_credits/:edition_id
  def destroy
    @credit = EditionCredit.find(params[:id])

    #Hack to get around after_delete callback issues
    entry = Hash.new
    entry.store("Removed", [@credit.artist.name, @credit.edition.title])
    Edit.create_edit(entry, @credit.artist)

    #Attempt to delete credit
	respond_to do |format|
      if @credit.destroy
        flash[:notice] = 'Credit successfully removed.'
        format.html { redirect_to_request_uri }
		format.js {
		  render :update do |page|
		    page.remove("edition-credit-#{params[:id]}")
		  end
		}
      else
        flash[:errors] = 'Credit was not able to be deleted, please try again.'
        redirect_to_request_uri
      end
	end
  end
end
